package com.ei.itop.common.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ailk.dazzle.util.sec.Encrypt;
import com.ei.itop.common.bean.OpInfo;
import com.ei.itop.common.constants.SysConstants;

public class LoginUtil {

	public static void putLoginInfo2Session(HttpServletRequest request,
			HttpServletResponse response, String loginCode, long opId,
			String opCode, String opName, String opEnName, String opType,
			long orgId, String orgName, Long custId, Long opKind,String loginPwd,String loginRemember) {
		// 封装OP信息放入Session中
		OpInfo op = new OpInfo();
		op.setOpCode(opCode);
		op.setOpId(opId);
		op.setOpName(opName);
		op.setOpFullName(opEnName+"/"+opName);
		op.setOpType(opType);
		op.setOrgId(orgId);
		op.setOrgName(orgName);
		op.setCustId(custId + "");
		op.setOpKind(opKind == null ? 0 : opKind);
		request.getSession().setAttribute(
				SysConstants.SessionAttribute.OP_SESSION, op);
		// 将操作员的登录类型放入Cookie，方便下次登录
		Cookie cookieOpType = new Cookie(SysConstants.CookieAttribute.OP_TYPE,
				opType);
		cookieOpType.setMaxAge(Integer.MAX_VALUE);
		Cookie cookieAccountNo = new Cookie(
				SysConstants.CookieAttribute.ACCOUNT_NO, loginCode);
		cookieAccountNo.setMaxAge(Integer.MAX_VALUE);
		response.addCookie(cookieOpType);
		response.addCookie(cookieAccountNo);
		if("1".equals(loginRemember)){
			Cookie loginKeyCookie = new Cookie(
					SysConstants.CookieAttribute.LOGIN_KEY, Encrypt.encrypt(opType+SysConstants.CookieAttribute.LOGIN_KEY_SEP+loginCode+SysConstants.CookieAttribute.LOGIN_KEY_SEP+loginPwd));
			loginKeyCookie.setMaxAge(3600*24*30);
			response.addCookie(loginKeyCookie);
		}
	}
	
}
